package com.java.Tomcat;

import java.io.File;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;

public class MyHttpServletRequest {
    private Socket socket;
    private InputStream inputStream;
    private String protocol; // 协议
    private String scheme; // 协议类型
    private String method; // 请求方法
    private String realPath; // 真实路径
    private String requestURI; // 统一资源标识符
    private String requestURL; // 统一资源定位符
    private String contextPath; // 上下文路径
    private String queryString; // 地址栏参数
    private Map<String, String[]> parameterMap = new ConcurrentHashMap<>(); // 请求参数

    public MyHttpServletRequest(Socket socket, InputStream inputStream) {
        this.socket = socket;
        this.inputStream = inputStream;

        // 解析HTTP请求
        parseHttpRequest();
    }

    private void parseHttpRequest() {
        // 解析输入流所有内容
        String inputStreamStr = parseInputStreamStr();
        if (inputStreamStr == null || "".equals(inputStreamStr)) {
            throw new RuntimeException("读取输入流异常");
        }

        // 解析请求参数
        parseHttpRequest(inputStreamStr);
    }

    private String parseInputStreamStr() {
        // 读取输入流所有数据，只考虑普通文本
        int length = -1;
        byte[] buffer = new byte[100 * 1024];
        try {
            length = inputStream.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
            length = 0;
        }

        // 拼接协议
        StringBuffer stringBuffer = new StringBuffer(100 * 1024);
        for (int i = 0; i < length; i++) {
            stringBuffer.append((char) buffer[i]);
        }
        return stringBuffer.toString();
    }

    private void parseHttpRequest(String inputStreamStr) {
        // 字符串分割器根据自然分隔符分割
        StringTokenizer stringTokenizer = new StringTokenizer(inputStreamStr);

        this.method = stringTokenizer.nextToken();

        this.requestURI = stringTokenizer.nextToken();
        int queryIndex = this.requestURI.indexOf("?");
        if (queryIndex != -1) {
            this.requestURI = this.requestURI.substring(0, queryIndex);
            this.queryString = this.requestURI.substring(queryIndex + 1);
        }

        this.protocol = stringTokenizer.nextToken();
        this.scheme = this.protocol.substring(0, this.protocol.indexOf("/"));

        int slashIndex = this.requestURI.indexOf("/", 1);
        if (slashIndex != -1) {
            this.contextPath = this.requestURI.substring(0, slashIndex);
        } else {
            this.contextPath = this.requestURI;
        }

        this.requestURL = this.scheme + "://" + this.socket.getRemoteSocketAddress() + ":" + this.requestURI;

        // 此处没有考虑表单提交的参数
        if (this.queryString != null && this.queryString.length() > 0) {
            String[] querys = this.queryString.split("&");
            for (String query : querys) {
                String[] params = query.split("=");
                this.parameterMap.put(params[0], params[1].split(","));
            }
        }

        this.realPath = System.getProperty("user.dir") + File.separator + "webapps";
    }

    public Socket getSocket() {
        return socket;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getScheme() {
        return scheme;
    }

    public String getMethod() {
        return method;
    }

    public String getRealPath() {
        return realPath;
    }

    public String getRequestURI() {
        return requestURI;
    }

    public String getRequestURL() {
        return requestURL;
    }

    public String getContextPath() {
        return contextPath;
    }

    public String getQueryString() {
        return queryString;
    }

    public Map<String, String[]> getParameterMap() {
        return parameterMap;
    }
}
